/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pneuservis.backend.services.impl;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Stateless;
import org.apache.shiro.authz.annotation.RequiresRoles;
import pneuservis.backend.dao.TyreTypeDAOLocal;
import pneuservis.backend.entities.TyreType;
import pneuservis.backend.others.Secured;
import pneuservis.backend.services.TyreTypeServiceLocal;
import pneuservis.backend.to.TyreTypeTO;


/**
 *
 * @author Lukas Stevko
 */
@Stateless(name="TyreTypeService", mappedName="TyreTypeService")
@Local(value = TyreTypeServiceLocal.class)
public class TyreTypeService implements TyreTypeServiceLocal {
    
    @EJB
    private TyreTypeDAOLocal tyreTypeDAO;
    
    @Override
//    @Secured
//    @RequiresRoles("admin")
    public void createTyreType(TyreTypeTO tyreType) {
        TyreType tyre = tyreType.toTyreType();
        tyreTypeDAO.createTyreType(tyre);
        tyreType.setId(tyre.getId());
    }
    
    @Override
    public TyreTypeTO getTyreType(Long id) {
        TyreTypeTO result;
        result = new TyreTypeTO(tyreTypeDAO.getTyreType(id));
        return result;
    }
    
    @Override
    @Secured
    @RequiresRoles("admin")
    public void updateTyreType(TyreTypeTO tyreTypeDao) {
        tyreTypeDAO.updateTyreType(tyreTypeDao.toTyreType());
    }
    
    @Override
//    @Secured
//    @RequiresRoles("admin")
    public void removeTyreType(TyreTypeTO tyreTypeDao) {
        tyreTypeDAO.removeTyreType(tyreTypeDao.toTyreType());
        tyreTypeDao.setId(null);
    }
    
    @Override
    public List<TyreTypeTO> getAllTyreTypes() {
        List<TyreType> tyreTypeList;
        tyreTypeList = tyreTypeDAO.getAllTyreTypes();
        List<TyreTypeTO> result = new ArrayList<TyreTypeTO>();
        for (TyreType t : tyreTypeList) {
            result.add(new TyreTypeTO(t));
        }
        return result;
    }
    
    @Override
    public List<TyreTypeTO> findTyreTypesByRimSize(int rimSize) {
        List<TyreType> tyreTypeList;
        tyreTypeList = tyreTypeDAO.findTyresByRimSize(rimSize);
        List<TyreTypeTO> result = new ArrayList<TyreTypeTO>();
        for (TyreType t : tyreTypeList) {
            result.add(new TyreTypeTO(t));
        }
        return result;
    }
    
    @Override
    public List<TyreTypeTO> findTyreTypesByWidth(int width) {
        List<TyreType> tyreTypeList;
        tyreTypeList = tyreTypeDAO.findTyresByWidth(width);
        List<TyreTypeTO> result = new ArrayList<TyreTypeTO>();
        for (TyreType t : tyreTypeList) {
            result.add(new TyreTypeTO(t));
        }
        return result;
    }
    
    @Override
    public List<TyreTypeTO> findTyreTypesByProfile(int profile) {
        List<TyreType> tyreTypeList;
        tyreTypeList = tyreTypeDAO.findTyresByProfile(profile);
        List<TyreTypeTO> result = new ArrayList<TyreTypeTO>();
        for (TyreType t : tyreTypeList) {
            result.add(new TyreTypeTO(t));
        }
        return result;
    }
    
    @Override
    public List<TyreTypeTO> findTyreTypesByManufacturer(String manufacturer) {
        List<TyreType> tyreTypeList;
        tyreTypeList = tyreTypeDAO.findTyresByManufacturer(manufacturer);
        List<TyreTypeTO> result = new ArrayList<TyreTypeTO>();
        for (TyreType t : tyreTypeList) {
            result.add(new TyreTypeTO(t));
        }
        return result;
    }
}
